<?php

require_once ('calc.php');
require_once ('db.php');
$db = getDb();

$content=file_get_contents("../view/details.html");
$id = $_GET['id'];

$_SUBJECT_="_SUBJECT_";
$_SUBJECT_content="";
$_TO_="_TO_";
$_TO_content="";
$_START_DATE_="_START_DATE_";
$_START_DATE_content="undef";
$_LENGTH_="_LENGTH_";
$_LENGTH_content="";
$_MESSAGE_="_MESSAGE_";
$_MESSAGE_content="";
$_ATTACHEMENTS_="_ATTACHEMENTS_";
$_ATTACHEMENTS_content="";
$_RESPONSES_="_RESPONSES_";
$_RESPONSES_content="";
$_BEST_TIME_="_BEST_TIME_";
$_BEST_TIME_content="";

$toValues="";


$sqlCommand="SELECT subject,message,startDate, length FROM subevent WHERE id=". $id;
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	
	$_SUBJECT_content = $result->fields[0];
	$_MESSAGE_content = $result->fields[1];
	$_START_DATE_content= $result->fields[2];
	$_LENGTH_content= $result->fields[3];
	//TO
	
	$sqlCommand="SELECT e.address,e.name, e.id FROM email e JOIN sentsingle ss ON e.id=ss.emailid WHERE ss.subeventid=".$id." AND type=0";
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$toValues .= "<input type=\"hidden\" name=\"to[ ]\"  value=\"e".$result->fields[2]."\" />\n";
		$_TO_content .= "<option>". $result->fields[0]  . "(". $result->fields[1]  .")</option>\n";
		$result->MoveNext();
	}	

	// Attachments
	$sqlCommand="SELECT orgFilename, path FROM attachment WHERE orgFilename !=\"\" AND subeventid=" . $id;
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$_ATTACHEMENTS_content .= "<a href=\"". $result->fields[1] . "\">".$result->fields[0]. "</a><br/>\n";
		$result->MoveNext();
	}
	
	// Responses
	$sqlCommand="SELECT e.address,e.name, d.id FROM email e JOIN userData d ON e.id = d.emailId WHERE d.subeventId=". $id;
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$mail = $result->fields[0]  . "(". $result->fields[1]  .")";
		
		$_RESPONSES_content .= "<li><a href=\"showSchedule.php?id=". $result->fields[2]  ."\">".$mail."</a></li>\n";
		$result->MoveNext();
	}
	
	////// best time
	
	$doneMessage="";
	
	$sqlCommand="SELECT done FROM subevent WHERE id=". $id;
	$result = $db->Execute($sqlCommand);
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$doneMessage=$result->fields[0];
		$result->MoveNext();
	}
	
	if($doneMessage != "")
	{
		$_BEST_TIME_content .= $doneMessage;
	}
	else if($_RESPONSES_content=="")
	{
		$_BEST_TIME_content="No responses yet";
	}
	else
	{	
		$allDayData=json_decode($data);
		
		$tmp = json_decode($data);
		$dayData = $tmp[0];
		echo($dayData."<br>");
		
		
		print_r(json_decode($dayData));
		
	
		// best time
		
		$sumArray = array();
		for($i =0;$i<7*100;$i++)
			$sumArray[$i]=0;
		$dataForHour = round(100/17);
		
		
		$sqlCommand="SELECT data FROM userData WHERE subeventId=". $id;
		$result = $db->Execute($sqlCommand);
		
		if ($result === false)
			die($sqlCommand);
		while (!$result->EOF)
		{
			$allDayData=json_decode($result->fields[0]);
			for($i =0;$i<7;$i++)
			{
				$singleDayData= json_decode($allDayData[$i]);
				$singleSum = getMovAvgArray($singleDayData,$_LENGTH_content * $dataForHour );
				
				for($s =0;$s<count($singleSum);$s++)
					{
						$sumArray[$i*100 + $s] += $singleSum[$s];
					}
				
				
			}
			
			$result->MoveNext();
		}
		
		$indexes=array();
		
		for($z=0;$z<3;$z++)
		{
			$minIndex = array_search(min($sumArray), $sumArray);
			$indexes[$z]=$minIndex;
			
			for($i=0;$i<$dataForHour;$i++)
				{
					if($minIndex +$i > count($sumArray))
						break;
					
					$sumArray[$minIndex +$i] = 999999;
						
				}
			
			for($i=0;$i<$dataForHour;$i++)
				{
					if($minIndex -$i < 0)
						break;
					$sumArray[$minIndex -$i] = 999999;
				
				}	
				
		}
		
		
		for($z=0;$z<count($indexes);$z++)
		{
			$dayNumber = floor($indexes[$z]/100);
			$minutIndex = $indexes[$z]-(100*$dayNumber);
			$minuteQuarters = round((((17*60 * $minutIndex /100) + 6*60)/15));
			
			
			$timeToSend= date('H:i', mktime(0,$minuteQuarters*15));
			$dayToSend = date('Y-m-d', strtotime($_START_DATE_content .' +'. $dayNumber .' day'));
			
			
			
			$c = "Meeting at ". $dayToSend . " ". $timeToSend . 
			"\n<form action=\"setMeeting.php\" method=\"post\" enctype=\"multipart/form-data\">\n
			<input type=\"hidden\" name=\"id\" value=\"2\" />\n
			<input type=\"hidden\" name=\"subeventId\" value=\"". $id . "\" />\n
			<input type=\"hidden\" name=\"subject\" value=\"Meeting created\"/>\n
			".$toValues.
			"<input type=\"hidden\" name=\"message\"  value=\"Meeting set to ".$dayToSend . " ". $timeToSend ."\"/>\n
 			<input type=\"submit\" value=\"Set\"></form>\n";
			
			
			
			$_BEST_TIME_content .= $c;
			
		}
		
	}
	

 $content = str_replace($_TO_ , $_TO_content, $content);
 $content = str_replace($_START_DATE_ , $_START_DATE_content, $content);
 $content = str_replace($_LENGTH_ , $_LENGTH_content, $content);
 $content = str_replace($_MESSAGE_ , $_MESSAGE_content, $content);
 $content = str_replace($_ATTACHEMENTS_ , $_ATTACHEMENTS_content, $content);
 $content = str_replace($_SUBJECT_ , $_SUBJECT_content, $content);
 $content = str_replace($_RESPONSES_ , $_RESPONSES_content, $content);
 $content = str_replace($_BEST_TIME_ , $_BEST_TIME_content, $content);
 
 
 // Display page
 echo $content;
 
?>
